package com.zhj.jvm.test.ref;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhj
 */
public class SoftReferenceDemo {
    public static void main(String[] args) throws InterruptedException {
        // 装饰模式
        SoftReference<SoftObject> softRef = new SoftReference<>(new SoftObject());
        List<byte[]> list = new ArrayList<>();

        new Thread(()->{
            while (true) {
                SoftObject softObject = softRef.get();// 软引用对象
                if (softObject == null) {
                    System.out.println("软引用对象已被回收");
                    System.exit(0);
                }
            }
        },"A").start();

        while (true) {
            Thread.sleep(10);
            list.add(new byte[1024*1024]);
        }
    }
}
